package xj.graph2d.template.uml;

import static javax.swing.SwingConstants.LEFT;

import java.util.Map;

import xj.graph2d.AbstractModelListener;
import xj.graph2d.DrawShape;
import xj.graph2d.GraphEdgeBase;
import xj.graph2d.Label;
import xj.graph2d.TextShape;
import xj.graph2d.shapes.BoxLabel;
import xj.graph2d.template.UMLTemplate;

public abstract class UMLAbstractModelListener extends AbstractModelListener {

  public UMLAbstractModelListener() {
  }

  public UMLAbstractModelListener(UMLTemplate template) {
    this(null, null, template);
  }

  public UMLAbstractModelListener(Object model,
				  DrawShape shape,
				  UMLTemplate template) {
    super(model, shape);
    this.template = template;
  }

  public UMLAbstractModelListener(Object model, 
				  DrawShape shape, 
				  Map iconMap,
				  UMLTemplate template) {
    super(model, shape, iconMap);
    this.template = template;
  }

  protected Label setLabel(GraphEdgeBase edge, int role, String label,
			   String prompt, boolean multi, String fontkey, String colorkey) {
    return setLabel(edge, role, label, prompt, null, null, multi, fontkey, colorkey);
  }

  protected Label setLabel(GraphEdgeBase edge, int role, String label,
			   String prompt, String prefix, String suffix, 
			   boolean multi,  String fontkey, String colorkey) {
    if (edge != null) {
      Label curLabel = edge.getLabelShape(role);
      if (curLabel != null) {
	if (label != null && label.length() > 0) {
	  if (curLabel instanceof TextShape) {
	    curLabel.setText(label);
	    return curLabel;
	  } else {
	    edge.removeLabel(curLabel);
	  }
	} else {
	  edge.removeLabel(curLabel);
	  return null;
	}
      }

      if (label != null && label.length() > 0) {
	TextShape labelShape;
	if (template != null) {
	  labelShape = template.makeContentText(label, prompt, prefix, suffix,
						multi, fontkey, colorkey);
	} else {
	  labelShape = new TextShape(label);
	  labelShape.setPrompt((prompt != null) ? prompt : "text");
	  labelShape.setAllowMultiLine(multi);
	  labelShape.setDetachable(false);
	  labelShape.setScalable(false);
	  labelShape.showCursor(true);
	}
	labelShape.setMovable(true);
	labelShape.setSticky(false);
	labelShape.setAllowRotation(false);

	edge.setLabel(role, labelShape);
	edge.doLayout();
	return labelShape;
      }
    }
    return null;
  }

  protected Label setLabel(GraphEdgeBase edge, int role, int orientation,
			   String[] labels, String[] prompt, String[] prefix, String[] suffix,
			   String fontkey, String colorkey) {
    if (edge != null) {
      Label curLabel = edge.getLabelShape(role);
      if (curLabel != null) {
	if (!(curLabel instanceof BoxLabel)) {
	  edge.removeLabel(curLabel);
	}
      }

      if (labels != null && labels.length > 0) {
	BoxLabel box;
	boolean newbox = true;
	if (curLabel instanceof BoxLabel) {
	  box = (BoxLabel) curLabel;
	  box.removeAllShapes();
	  newbox = false;
	} else {
	  box = new BoxLabel(orientation, LEFT);
	  newbox = true;
	}
	TextShape prev = null;
	TextShape labelShape;
	for (int i = 0; i < labels.length; i++) {
	  if (template != null) {
	    labelShape = template.makeContentText(labels[i], prompt[i],
						  prefix[i], suffix[i], false, 
						  fontkey, colorkey);
	  } else {
	    labelShape = new TextShape(labels[i], prefix[i], suffix[i]);
	    labelShape.setPrompt((prompt[i] != null) ? prompt[i] : "text");
	    labelShape.setAllowMultiLine(false);
	    labelShape.setDetachable(false);
	    labelShape.setScalable(false);
	    labelShape.showCursor(true);
	  }
	  labelShape.setMovable(true);
	  labelShape.setSticky(false);
	  labelShape.setAllowRotation(false);
	  labelShape.setDisplayName(prompt[i]);
	  if (prev != null) {
	    labelShape.linkAfter(prev);
	  }
	  prev = labelShape;
	  box.addShape(labelShape);
	}

	box.setDetachable(false);
	box.setScalable(false);
	box.showCursor(true);
	box.setMovable(true);
	box.setSticky(false);
	box.setAllowRotation(false);

	box.doLayout();
	if (newbox) {
	  edge.setLabel(role, box);
	}
	edge.doLayout();
	return box;
      }

    }
    return null;
  }

  protected UMLTemplate template;

}
